package com.lindroy.jetpacknotes.navigation.navigationui

import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.NavigationUI
import com.lindroy.jetpacknotes.R

class NavigationUIActionBarActivity : AppCompatActivity() {

    private lateinit var navController: NavController

    private val appBarConfiguration by lazy {
        AppBarConfiguration.Builder(navController.graph)
            .build()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_navigation_ui_action)
        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.nav_host_fragment_ui) as NavHostFragment
        navController = navHostFragment.navController
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration)

        /* clRoot.post {
             navController = Navigation.findNavController(this, R.id.nav_host_fragment_ui)
             NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration)
             navController.addOnDestinationChangedListener { controller, destination, arguments ->
                 Log.d("Tag", "destination id：${destination.id}")
             }
         }*/
        /**
         * 直接这样用，会报错：
         * java.lang.IllegalStateException: Activity com.lindroy.jetpacknotes.navigation.navigationui.NavigationUIActionBarActivity@b47c5fe does not have a NavController set on 2131231231
         * 1、可以在 post 中初始化 navController，
         * 2、使用 supportFragmentManager.findFragmentById
         * 3、将 fragment 替换成 fragment
         * 参考文章：https://www.cnblogs.com/guanxinjing/p/14776047.html
         * 源码解释：https://blog.csdn.net/linminghuo/article/details/119000601
         */
//        navController =   findNavController( R.id.nav_host_fragment_ui)

        /* NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration)
         navController.addOnDestinationChangedListener { controller, destination, arguments ->
             Log.d("Tag", "destination id：${destination.id}")
         }*/
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        super.onCreateOptionsMenu(menu)
        menuInflater.inflate(R.menu.menu_settings, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean =
        NavigationUI.onNavDestinationSelected(item, navController) || super.onOptionsItemSelected(
            item
        )

    override fun onSupportNavigateUp(): Boolean = NavigationUI.navigateUp(
        navController,
        appBarConfiguration
    ) || super.onSupportNavigateUp()

    /**
     * 显示 Menu 的Icon
     */
   /* override fun onPrepareOptionsPanel(view: View?, menu: Menu): Boolean {
        if (menu.javaClass.simpleName == "MenuBuilder") {
            try {
                val m: Method = menu.javaClass.getDeclaredMethod(
                    "setOptionalIconsVisible",
                    java.lang.Boolean.TYPE
                )
                m.isAccessible = true
                m.invoke(menu, true)
            } catch (e: Exception) {
                Log.e(
                    javaClass.simpleName,
                    "onMenuOpened...unable to set icons for overflow menu",
                    e
                )
            }
        }
        return super.onPrepareOptionsPanel(view, menu)
    }*/


}